/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 *	ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
#include <iostream>

#include "common_list.h"

using namespace std;

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param head ListNode类
     * @return ListNode类
     */
    ListNode *ReverseList(ListNode *head) {
        ListNode *pre, *cur, *next;
        pre = nullptr;
        cur = next = head;

        while (cur) {
            next      = cur->next;
            cur->next = pre;
            pre       = cur;
            cur       = next;
        }
        return pre;
    }
    ListNode *tail = nullptr;
    ListNode *do_ReverseListResc(ListNode *head) {
        if (!head || !(head->next)) {
            tail = head;
            return head;
        }
        ListNode *next = do_ReverseListResc(head->next);
        next->next     = head;
        return head;
    }

    ListNode *ReverseListResc(ListNode *head) {
        tail = nullptr;
        if (!head || !(head->next)) {
            return head;
        }
        ListNode *new_tail = do_ReverseListResc(head);
        new_tail->next     = nullptr;
        return tail;
    }
};

int main(void) {
    cout << "翻转链表" << endl;
    ListNode *head = generate_list(0);
    print_list(head);
    head = (new Solution())->ReverseListResc(head);
    // head = (new Solution())->ReverseList(head);
    print_list(head);
    cout << "debug << " << head << endl;
    return 0;
}